package com.lingyuan.oms.listener;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.lingyuan.oms.constant.OrderConstant;
import com.lingyuan.oms.constant.OrderStatusEnum;
import com.lingyuan.oms.constant.RabbitMqConstant;
import com.lingyuan.oms.domain.OrderEntity;
import com.lingyuan.oms.domain.param.CreateOrderParam;
import com.lingyuan.oms.remote.WarehouseRemoteService;
import com.lingyuan.oms.service.OrderItemService;
import com.lingyuan.oms.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName：SubmitOrderListener
 * @Author: LeiHao
 * @Date: 2024/8/29 16:54
 * @Description: 生成订单监听
 */
@Component
@Log4j2
public class SubmitOrderListener {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OrderService orderService;
    @RabbitListener(queues = RabbitMqConstant.QUEUE_SUBMIT_DEAD_NAME)
    public void submitOrderListener(String createOrderParamStr, Channel channel, Message message) {
        OrderEntity order = null;
        CreateOrderParam createOrderParam = null;
        try {
            if (stringRedisTemplate.hasKey(RabbitMqConstant.SUBMIT_ORDER_MESSAGE_KEY+message.getMessageProperties().getMessageId())) {
                //确认消费
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                return;
            }
            log.info("开始生成订单：{}", createOrderParamStr);
            createOrderParam = JSON.parseObject(createOrderParamStr, CreateOrderParam.class);
            order = orderService.createOrder(createOrderParam);
            stringRedisTemplate.opsForValue().set(OrderConstant.SUBMIT_ORDER_KEY+createOrderParam.getOrderSn(), JSON.toJSONString(order));
            log.info("生成订单成功：{}", order);
            //防止重复消费
            stringRedisTemplate.opsForValue().set(RabbitMqConstant.SUBMIT_ORDER_MESSAGE_KEY+message.getMessageProperties().getMessageId(), createOrderParamStr, 6, TimeUnit.HOURS);
            //确认消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            //声明为无效订单添加到redis中去
            order.setStatus(OrderStatusEnum.INVALID.getCode());
            order.setOrderSn(createOrderParam.getOrderSn());
            stringRedisTemplate.opsForValue().set(OrderConstant.SUBMIT_ORDER_KEY+createOrderParam.getOrderSn(), JSON.toJSONString(order));
            log.error("生成订单失败：{}", e.getMessage());
            e.printStackTrace();
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
